package com.yunshang.shoppingmall.controller;


import com.yunshang.shoppingmall.common.MessageConstants;
import com.yunshang.shoppingmall.common.Response;
import com.yunshang.shoppingmall.entity.Config;
import com.yunshang.shoppingmall.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.context.request.WebRequest;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.BufferedReader;
//import java.io.DataOutputStream;
//import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.util.HashMap;
//import java.util.Map;
@Controller
public class ConfigController {

    @Autowired
    ConfigService configService;
    //获取配置信息
    @ResponseBody
    @RequestMapping("/getConfig")
    public Response getConfig(Model model)
    {
        Config config=configService.selectByPrimaryKey(1);
        //model.addAttribute("config",config);
        //return "1";
        return new Response(MessageConstants.MSG_SUCCESS,config,"");
    }
    //更新配置信息
    @RequestMapping("/updateConfig")
    public Response updateConfig()
    {
        Config config=new Config();
        Integer res=configService.updateByPrimaryKey(config);
        return new Response(MessageConstants.MSG_SUCCESS,-1,"");
    }
    private final static String ENCODE = "GBK";

    public Config getConfig()
    {
        return configService.selectByPrimaryKey(1);

    }

//    @ResponseBody
//    @RequestMapping("/SMSAPI")
//    //发送短信接口
//    public Response SMSAPI(String phone,String template,String strContent)
//    {
//        Config config = getConfig();
////        String strContent = "{\"name\":\"yangxk\"}";
////        String name= null;
////        try {
////            name = java.net.URLEncoder.encode("创始合伙人",ENCODE);
////        } catch (UnsupportedEncodingException e) {
////            e.printStackTrace();
////        }
//        String data="?ac=send&uid="+config.getSmsAccount()+"&pwd="+config.getSmsPwd()+"&template="+template+"&mobile="+phone+"&content="+strContent;
//        byte[] b= new byte[0];
//        try {
//            b = data.getBytes("UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        String res=configService.doPostRequest("http://api.sms.cn/sms/"+data,b);
//        return new Response(MessageConstants.MSG_SUCCESS,res,"");
//    }

    //发送短信接口
    public String SMSAPI(String phone,String template,String strContent)
    {
        Config config = getConfig();
        System.out.println(config.getSmsAccount());
        System.out.println(config.getSmsPwd());
        String data="?ac=send&uid="+config.getSmsAccount()+"&pwd="+config.getSmsPwd()+"&template="+template+"&mobile="+phone+"&content="+strContent;
        byte[] b= new byte[0];
        try {
            b = data.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String res=configService.doPostRequest("http://api.sms.cn/sms/"+data,b);
        return res;
    }

    public String SMSAPI_code(String phone,String codeValue)
    {
        String strContent = "{\"code\":\""+codeValue+"\"}";
        return SMSAPI(phone,"402001",strContent);
    }

    public ConfigController() {
    }
}
